var bg = document.createElement("div");
bg.setAttribute("id", "spa_bg");
bg.setAttribute("style", "display:none;"
	+"position:fixed;"
	+"top:0px;"
	+"left:0px;"
	+"height:100%;"
	+"width:100%;"
	+"background-color:black;"
	+"opacity:0.5;"
);

var fg = document.createElement("div");
fg.setAttribute("id", "spa_fg");
fg.setAttribute("style", "display:none;"
	+"position:fixed;"
	+"top:100px;"
	+"left:25%;"
	+"width:50%;"
	+"background-color:#E5ECF9;"
	+"-webkit-border-radius: 10px;"
	+"border: 1px solid black;"
	+"padding: 10px;"
	+"color: black;"
);

var fg_headline = document.createElement("div");
fg_headline.setAttribute("id","spa_headline");
fg_headline.setAttribute("style","font-weight:bold;"
	+"border: 1px solid black;"
	+"border-width: 0px 0px 1px 0px;"
	+"padding-bottom: 5px;"
);

var fg_content = document.createElement("div");
fg_content.setAttribute("id","spa_content");
fg_content.setAttribute("style","padding-top: 5px;"
	+"padding-bottom: 5px;"
);

var fg_footer = document.createElement("div");
fg_footer.setAttribute("id","spa_footer");
fg_footer.setAttribute("style","border: 1px solid black;"
	+"border-width: 1px 0px 0px 0px;"
	+"padding-top: 5px;"
);
fg_footer.innerHTML = ""
+"Click on the link to subscribe | "
+"Click on subscribr icon again to close";

function find() {
  if (window == top) {
  	var port = chrome.extension.connect();
	
	port.onMessage.addListener(function(data) {
		try {
			if(data.substr && data.substr(0,5)=="Debug")	{
				alert(data);
			}
			fg_headline.innerHTML = "Subscribr found " + data.length + " feeds";
			fg.appendChild(fg_headline);
			for (var feed in data) {
				var path = chrome.extension.getURL(data[feed].icon);
				fg_content.innerHTML += ""
					+"<img src='"+path+"' border=0 />&nbsp;"
					+"<a href='" + data[feed].href + "' style='color: black;'>"
						+data[feed].title +
					"</a>" +
				"<br />";
			}
			fg.appendChild(fg_content);
			fg.appendChild(fg_footer);
			if (fg.style.display == "none") {
				var body = document.getElementsByTagName("body");
				body[0].appendChild(bg);
				body[0].appendChild(fg);
				
				var els = document.getElementsByTagName("*");
				var z = 1;
				for(var test in els)	{
					if (els[test].style)	{
						var css = document.defaultView.getComputedStyle(els[test], null);
						if (css["zIndex"].toLowerCase()!="auto" && css["zIndex"] > z) {
							z = css["zIndex"];
						}
					}
				}

				bg.style.display = "block";
				bg.style.zIndex = z+1;
				fg.style.display = "block";
				fg.style.zIndex = z+2;
			}
			else {
				fg_headline.innerHTML = "";
				fg_content.innerHTML = "";
				document.getElementById("spa_bg").style.display = "none";
				document.getElementById("spa_fg").style.display = "none";
			}
		} 
		catch (e) {
			alert(e.message);
		}
	});
	
    // Find all the RSS link elements.
    var result = document.evaluate(
        '//link[@rel="alternate"][contains(@type, "rss") or ' +
        'contains(@type, "atom") or contains(@type, "rdf")]',
        document, null, 0, null);

    var feeds = [];
    var item;
    while (item = result.iterateNext()) {
		feeds.push({
			href: item.href,
			title: item.title
		});
	}
	if (feeds.length > 0) {
		port.postMessage(feeds);
	}
  }
}

find();
window.addEventListener("focus", find);